package com.nixus.raop;

import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.nixus.raop.core.ServiceContextImpl;

public class MainActivity extends Activity implements OnClickListener,Observer{
	/** Called when the activity is first created. */




	protected static LibraryAdapter discoveredDevicesAdapter;
	private NotificationManager mNotificationManager;
	private Builder mBuilder;


	private RaopHelper raopHelper;
	


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ToggleButton tb = (ToggleButton) findViewById(R.id.toggleButton1);
		tb.setOnClickListener(this);

		ListView discoveredDevicesList = (ListView)findViewById(R.id.list);
		discoveredDevicesAdapter = new LibraryAdapter(this);
		discoveredDevicesList.setAdapter(discoveredDevicesAdapter);

		initNotifications();

	    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
			
			public void run() {
				RaopHelper.setRaopSwitch("O");
			}
		}));
	    
	    this.raopHelper = new RaopHelper();
	    this.raopHelper.addObserver(this);
		this.raopHelper.startDeviceDiscovery();

	}


	private void initNotifications() {
		mBuilder =
		        new NotificationCompat.Builder(this)
		        .setSmallIcon(R.drawable.airplay)
		        .setContentTitle("Waveplay notification");
		
		// Creates an explicit intent for an Activity in your app
		Intent resultIntent = new Intent(this, MainActivity.class);

		// The stack builder object will contain an artificial back stack for the
		// started Activity.
		// This ensures that navigating backward from the Activity leads out of
		// your application to the Home screen.
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
		// Adds the back stack for the Intent (but not the Intent itself)
		stackBuilder.addParentStack(MainActivity.class);
		// Adds the Intent that starts the Activity to the top of the stack
		stackBuilder.addNextIntent(resultIntent);
		PendingIntent resultPendingIntent =
		        stackBuilder.getPendingIntent(
		            0,
		            PendingIntent.FLAG_UPDATE_CURRENT
		        );
		mBuilder.setContentIntent(resultPendingIntent);
	    mNotificationManager =
		    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
	}








	public class LibraryAdapter extends BaseAdapter {

		protected Context context;
		protected LayoutInflater inflater;

		public View footerView;

		protected List<ServiceContextImpl> known = new LinkedList<ServiceContextImpl>();

		public LibraryAdapter(Context context) {
			this.context = context;
			this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void notifyFound(ServiceContextImpl context) {
			known.add(context);
		}

		public Object getItem(int position) {
			return known.get(position);
		}

		public boolean hasStableIds() {
			return true;
		}

		public int getCount() {
			return known.size();
		}

		public long getItemId(int position) {
			return position;
		}

		
		
		public View getView(int position, View convertView, ViewGroup parent) {

			
			final ServiceContextImpl serviceContextImpl = (ServiceContextImpl)this.getItem(position);
			final String airportName = serviceContextImpl.getProperty("name").replace("speaker.airport.", "");
			final String airportAddress = serviceContextImpl.getProperty("host");


			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			View rowView = inflater.inflate(R.layout.device_list_element, parent, false);
			TextView textView = (TextView) rowView.findViewById(R.id.textView1);
			final ToggleButton tb = (ToggleButton) rowView.findViewById(R.id.toggleButton1);

			textView.setText(airportName);

			final int mId = 0;
			
			tb.setOnClickListener(new OnClickListener() {

				
				public void onClick(View v) {
					if (tb.isChecked()){
						raopHelper.startStreaming(airportAddress);
					}
					else {
						raopHelper.stopStreaming(airportAddress);
					}
				}

				
			});

			return rowView;

		}


	}

	public void onClick(View arg0) {
		RaopHelper.setRaopSwitch("0");
		System.exit(0);
	}


	public void update(Observable observable, final Object data) {
		
		System.out.println("bbbbbb " + data.toString());

		if (data instanceof ServiceContextImpl){
			runOnUiThread(new Runnable() {
			     public void run() {
						discoveredDevicesAdapter.notifyFound((ServiceContextImpl)data);
						discoveredDevicesAdapter.notifyDataSetChanged();
			    }
			});

		}
		if (data instanceof String){
			mBuilder.setContentText("Streaming on " + data.toString());
			// mId allows you to update the notification later on.
			mNotificationManager.notify(0, mBuilder.build());
		}

		
	}


}